﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ProjectStorageDB;

namespace ProjectStorageLib.User
{
    public class UserProcessor : DatabaseEntities
    {

        #region Constructor and Destructor

        public UserProcessor()
            : base()
        { }

        protected override void Dispose(bool flag)
        {
            
        }

        #endregion Constructor and Destructor

        #region Methods

        public bool IsUserExist(string userName, string password)
        {
            try
            {
                return (from us in ent.User
                        where us.user_name == userName && us.password == password
                        select us).Count() > 0;
            }
            catch (Exception)
            {
                throw;
            }
        }

        public bool IsUserExist(string userName)
        {
            try
            {
                return (from us in ent.User
                        where us.user_name == userName
                        select us).Count() > 0;
            }
            catch (Exception)
            {
                throw;
            }
        }

        public ProjectStorageDB.User GetUserByUserNameAndPassword(string userName, string password)
        {
            try
            {
                return (from us in ent.User
                        where us.user_name == userName && us.password == password
                        select us).FirstOrDefault();
            }
            catch (Exception)
            {
                throw;
            }
        }

        public void AddUser(ProjectStorageDB.User user)
        {
            try
            {
                //atach user
                ent.AddToUser(user);

                //save data
                ent.SaveChanges();
            }
            catch (Exception)
            {
                throw;
            }
        }

        public bool? IsUserAdmin(decimal userID)
        {
            return (from us in ent.User where us.id_user == userID select us.is_admin).FirstOrDefault();
        }

        #endregion Methods
    }


}
